import { httpGet, httpPost, httpPut, httpDelete } from '@/utils/http';

// 类别接口类型定义
export interface Category {
  id: number;
  name: string;
  slug: string;
  description?: string;
  parent_id?: number;
  parent_name?: string;
  image?: string;
  icon?: string;
  sort_order: number;
  is_active: boolean;
  meta_title?: string;
  meta_description?: string;
  children_count?: number;
  products_count?: number;
  created_at: string;
  updated_at: string;
  children?: Category[];
}

export interface CategoryQueryParams {
  page?: number;
  limit?: number;
  keyword?: string;
  parent_id?: number | 'null';
  is_active?: boolean;
}

export interface CreateCategoryRequest {
  name: string;
  slug: string;
  description?: string;
  parent_id?: number;
  image?: string;
  icon?: string;
  sort_order?: number;
  is_active?: boolean;
  meta_title?: string;
  meta_description?: string;
}

export interface UpdateCategoryRequest extends Partial<CreateCategoryRequest> {}

export interface CategoryListResponse {
  items: Category[];
  pagination: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
}

// 获取类别列表
export const getCategoryList = async (params?: CategoryQueryParams) => {
  const queryParams = new URLSearchParams();
  
  if (params?.page) queryParams.append('page', params.page.toString());
  if (params?.limit) queryParams.append('limit', params.limit.toString());
  if (params?.keyword) queryParams.append('keyword', params.keyword);
  if (params?.parent_id !== undefined) queryParams.append('parent_id', params.parent_id.toString());
  if (params?.is_active !== undefined) queryParams.append('is_active', params.is_active.toString());

  const queryString = queryParams.toString();
  const url = `/admin/categories${queryString ? `?${queryString}` : ''}`;
  
  return httpGet<CategoryListResponse>(url);
};

// 获取类别树形结构
export const getCategoryTree = async (isActive?: boolean) => {
  const params = new URLSearchParams();
  if (isActive !== undefined) params.append('is_active', isActive.toString());
  
  const queryString = params.toString();
  const url = `/admin/categories/tree${queryString ? `?${queryString}` : ''}`;
  
  return httpGet<Category[]>(url);
};

// 获取类别详情
export const getCategoryDetail = async (id: number) => {
  return httpGet<Category>(`/admin/categories/${id}`);
};

// 创建类别
export const createCategory = async (data: CreateCategoryRequest) => {
  return httpPost<Category>('/admin/categories', data);
};

// 更新类别
export const updateCategory = async (id: number, data: UpdateCategoryRequest) => {
  return httpPut<Category>(`/admin/categories/${id}`, data);
};

// 删除类别
export const deleteCategory = async (id: number) => {
  return httpDelete(`/admin/categories/${id}`);
};

// 批量更新类别状态
export const batchUpdateCategoryStatus = async (ids: number[], isActive: boolean) => {
  return httpPost('/admin/categories/batch-update-status', {
    ids,
    is_active: isActive
  });
};

// 获取类别统计信息
export const getCategoryStatistics = async () => {
  return httpGet('/admin/categories/statistics/overview');
}; 